﻿// Copyright (c) Microsoft Corporation.  All rights reserved.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EdmMetadataHelpers;
using System.IO;

public partial class EditModel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            ShowModel(Request.QueryString["Model"]);
        }
    }
    protected void ShowModel(string modelName)
    {
        //Make ourselves visible

        if (modelName == null || modelName == "[Add a model]")
        {
            txtModel_Name.Text = "[New Model]";
            txtModel_Provider.Text = EdmWrapper.DEFAULT_PROVIDER;
            txtModel_ProviderConnectionString.Text = EdmWrapper.DEFAULT_PROVIDER_CONN_STR;
        }
        else
        {
            EdmWrapper edm = EdmWrappers.Current.FindByName(modelName);
            if (edm == null) throw new ArgumentNullException(string.Format("Couldn't find an Edm called: {0}", modelName));
            txtModel_Name.Text = edm.Name;
            txtModel_Provider.Text = edm.Provider;
            txtModel_ProviderConnectionString.Text = edm.ProviderConnectionString;
        }
    }
    protected void btnModelSave_Click(object sender, EventArgs e)
    {
        string name = txtModel_Name.Text.Trim();
        string provider = txtModel_Provider.Text.Trim();
        string providerConnectionString = txtModel_ProviderConnectionString.Text.Trim();

        
        EdmWrapper container = EdmWrappers.Current.FindByName(name);

        //We can't do anything if we are in create mode (i.e. the container doesn't yet exist) and we don't have all the files
        if (container == null && (!fileModel_Csdl.HasFile || !fileModel_Msl.HasFile || !fileModel_Ssdl.HasFile)) 
            return;

        //do we need to upload a new file?
        bool newCsdl = container == null || fileModel_Csdl.HasFile;
        bool newSsdl = container == null || fileModel_Ssdl.HasFile;
        bool newMsl = container == null || fileModel_Ssdl.HasFile;


        string csdlPath = newCsdl ? GetTempFilePath("csdl"): container.CsdlPath;
        string ssdlPath = newSsdl ? GetTempFilePath("ssdl") : container.SsdlPath;
        string mslPath = newMsl ? GetTempFilePath("msl") : container.MslPath;


        if (newCsdl) StoreFile(fileModel_Csdl, csdlPath);
        if (newSsdl) StoreFile(fileModel_Ssdl, ssdlPath);
        if (newMsl) StoreFile(fileModel_Msl,mslPath);

        if (container != null)
        {
            EdmWrappers.Current.Remove(container);
        }
        container = new EdmWrapper(name, provider, providerConnectionString, csdlPath, ssdlPath, mslPath);

        //Put at the start of collection
        EdmWrappers.Current.Insert(0,container);
        //Select the root node
        Response.Redirect("Default.aspx", true);

    }
    protected void btnModelCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx", true);
    }
    protected string GetTempFilePath(string extension)
    {
        return Path.GetTempFileName().Replace(".tmp", "." + extension);
    }
    protected void StoreFile(FileUpload control, string path)
    {
        StreamReader reader = new StreamReader(control.FileContent);
        StreamWriter writer = new StreamWriter(File.OpenWrite(path));
        writer.Write(reader.ReadToEnd());
        writer.Close();
        reader.Close();
    }
}
